/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.game.math.view.action;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Action;
import org.game.math.config.Configure;
import org.game.math.expression.Operator;

/**
 *
 * @author nmduc
 */
public class ActionManager {

    public static final String NEW_ACTION = "NEW_ACTION";
    public static final String CHECK_ACTION = "CHECK_ACTION";
    public static final String ADD_ACTION = "ADD_ACTION";
    public static final String SUB_ACTION = "SUB_ACTION";
    public static final String MUL_ACTION = "MUL_ACTION";
    public static final String DIV_ACTION = "DIV_ACTION";
    public static final String MUL_TABLE_ACTION = "MUL_TABLE_ACTION";
    public static final String PREFERENCE_ACTION = "PREFERENCE_ACTION";
    private Map<String, Action> actionMap = null;
    private Map<Operator, Action> actionOpMap = null;
    private static ActionManager actionManager;

    private ActionManager() {
        actionMap = new HashMap<String, Action>();
        actionOpMap = new EnumMap(Operator.class);
        Configure configure = Configure.getInstance();

        actionMap.put(NEW_ACTION, new NewGameAction());
        actionMap.put(CHECK_ACTION, new CheckGameAction());
        AddOperatorAction addOperatorAction = new AddOperatorAction(configure);
        actionMap.put(ADD_ACTION, addOperatorAction);
        actionOpMap.put(Operator.ADD, addOperatorAction);
        SubOperatorAction subOperatorAction = new SubOperatorAction(configure);
        actionMap.put(SUB_ACTION, subOperatorAction);
        actionOpMap.put(Operator.SUB, subOperatorAction);
        MulOperatorAction mulOperatorAction = new MulOperatorAction(configure);
        actionMap.put(MUL_ACTION, mulOperatorAction);
        actionOpMap.put(Operator.MUL, mulOperatorAction);
        DivOperatorAction divOperatorAction = new DivOperatorAction(configure);
        actionMap.put(DIV_ACTION, divOperatorAction);
        actionOpMap.put(Operator.DIV, divOperatorAction);
        MulTableAction mulTableAction = new MulTableAction(configure);
        actionMap.put(MUL_TABLE_ACTION, mulTableAction);
        PreferenceAction preferenceAction = new PreferenceAction();
        actionMap.put(PREFERENCE_ACTION, preferenceAction);

        configure.addObserver(addOperatorAction);
        configure.addObserver(subOperatorAction);
        configure.addObserver(mulOperatorAction);
        configure.addObserver(divOperatorAction);
    }

    public static ActionManager getInstance() {
        if (actionManager == null) {
            actionManager = new ActionManager();
        }
        return actionManager;
    }

    public Action getAction(String actionKey) {
        return actionMap.get(actionKey);
    }

    public Action getAction(Operator operator) {
        return actionOpMap.get(operator);
    }

}
